package cn.lingnan.controller.admin;

import cn.lingnan.pojo.dto.UserLoginDTO;
import cn.lingnan.pojo.entity.Admin;
import cn.lingnan.pojo.entity.User;
import cn.lingnan.pojo.vo.AdminLoginVO;
import cn.lingnan.pojo.vo.UserLoginVO;
import cn.lingnan.service.LoginService;
import cn.lingnan.utils.JwtUtils;
import cn.lingnan.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminLoginController {


    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    @CrossOrigin
    public Result<AdminLoginVO> userLogin(String account, String password) {
        log.info("管理员登陆:{},{}",account,password );
       Admin admin= loginService.AdminLogin(account,password);
        if(admin==null){
            return Result.error("用户名或密码错误");
        }
        log.info("管理员登陆成功:{}", admin);

        //登录成功后，生成jwt令牌

        //生成jwt令牌
        Map<String, Object> claims=new HashMap<>();
        claims.put("id",admin.getAdminId());
        claims.put("username",admin.getAdminName());
        String jwt=  JwtUtils.generateJwt(claims);

//        UserLoginVO userLoginVO= UserLoginVO.builder()
//                .userId(user.getUserId())
//                .userName(user.getUserName())
//                .phoneNumber(user.getPhoneNumber())
//                .token(jwt)
//                .build();
        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .adminId(admin.getAdminId())
                .adminName(admin.getAdminName())
                .token(jwt)
                .build();


        return Result.success(adminLoginVO);
    }

}
